﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Collections.Specialized;

namespace UMC.Web
{
    /// <summary>
    /// 表单对话框
    /// </summary> 
    public class UIFormDialog : UIDialog
    {
        List<WebMeta> _dataSource = new List<WebMeta>();
        /// <summary>
        /// 类型
        /// </summary>
        protected override string DialogType
        {
            get { return "Form"; }
        }
        public void Menu(string text, string model, string cmd, string value)
        {
            this.Menu(new UIClick(model, cmd, value) { Text = text });
        }
        public void Menu(string text, string model, string cmd)
        {
            this.Menu(new UIClick(model, cmd) { Text = text });
        }
        public void Menu(params UIClick[] menus)
        {
            this.Config.Put("menu", menus);
        }
        public void Menu(string text, string model, string cmd, WebMeta param)
        {
            this.Menu(new UIClick(model, cmd, param) { Text = text });
        }
        public WebMeta Add(UICell cell)
        {
            var webMeta = new WebMeta().Put("Type", cell.Type).Put("value", cell.Data).Put("format", cell.Formats).Put("style", cell.Style);
            this._dataSource.Add(webMeta);
            return webMeta;

        }
        /// <summary>
        /// 获取异步对话框的值
        /// </summary>
        /// <param name="asyncId">异步值Id</param>
        /// <param name="callback">对话框回调方法</param>
        /// <returns></returns>
        public static new WebMeta AsyncDialog(WebContext context, string asyncId, Func<String, UIDialog> callback)
        {
            return GetAsyncValue(context, asyncId, false, callback, false) as WebMeta;
        }
        public WebMeta AddSlider(string title, string Code, int defaultValue, int min, int max)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["DefaultValue"] = defaultValue.ToString();
            v["Type"] = "FieldSlider";
            v["Name"] = Code;
            v["Max"] = max.ToString();
            v["Min"] = min.ToString(); ;
            this._dataSource.Add(v);
            return v;
        }
        public WebMeta AddSlider(string title, string Code, int defaultValue)
        {
            return AddSlider(title, Code, defaultValue, 0, 100);
        }


        public UIFormDialog Action(UIClick value)
        {
            this.Config.Put("Action", value);
            return this;
        }
        public UIFormDialog Action(bool value)
        {
            if (value)
            {

                this.Config.Put("Action", true);
            }
            else
            {

                this.Config.Remove("Action");//, true);
            }
            return this;

        }
        /// <summary>
        /// 增加地址输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddAddress(string title, string Code, string defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["DefaultValue"] = defaultValue;
            v["Type"] = "Address";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        public WebMeta AddScore(string title, string Code, int defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v.Put("DefaultValue", defaultValue);
            v["Type"] = "Score";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 增加电话输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddPhone(string title, string Code, string defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["DefaultValue"] = defaultValue;
            v["Type"] = "Number";
            v["Vtype"] = "Phone";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 增加数字输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddNumber(string title, string Code, int? defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["DefaultValue"] = defaultValue.ToString();
            v["Type"] = "Number";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 把页面参数key值，传入提交
        /// </summary>
        /// <param name="key"></param>
        public void UIKey(string key = "UI")
        {
            this.Config.Put("UIKey", key);
        }

        public WebMeta Add(String type, String Code, String title, String defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["DefaultValue"] = defaultValue;
            v["Type"] = type;
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;

        }
        public void AddHeader(UIHeader header)
        {
            this.Config.Put("Header", header);
        }
        public void AddFooter(UIHeader header)
        {
            this.Config.Put("Footer", header);
        }


        /// <summary>
        /// 增加数字输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public void AddNumber(string title, string Code, decimal? defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            if (defaultValue.HasValue)
                v["DefaultValue"] = defaultValue.ToString();
            v["Type"] = "Number";
            v["Name"] = Code;
            this._dataSource.Add(v);
        }
        /// <summary>
        /// 增加数字输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddNumber(string title, string Code, float? defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            if (defaultValue.HasValue)
                v["DefaultValue"] = defaultValue.ToString();
            v["Type"] = "Number";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        public WebMeta AddScan(string title, string Code, string defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["DefaultValue"] = defaultValue;//.ToString();
            v["Type"] = "Scan";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 增加数字输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddNumber(string title, string Code, string defaultValue = "")
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["DefaultValue"] = defaultValue;
            v["Type"] = "Number";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 增加确认提示
        /// </summary>
        /// <param name="caption"></param>
        public void AddFooter(string caption, bool isform)
        {
            if (isform)
            {
                this.AddFooter(caption, null, (string)null);
            }
            else
            {
                this.AddFooter(caption);
            }
        }
        /// <summary>
        /// 尾部提示值
        /// </summary>
        /// <param name="caption"></param>
        public void AddFooter(string caption, string name, string defaultValue)
        {
            WebMeta v = new WebMeta();
            //v["Title"] = title;
            v["Text"] = caption;
            v["Type"] = "Confirm";
            v["DefaultValue"] = defaultValue ?? "YES";
            v["Name"] = name ?? "_FOOTER_NAME";
            this._dataSource.Add(v);
        }
        /// <summary>
        /// 尾部提示，
        /// </summary>
        /// <param name="text"></param>
        public void AddFooter(string text)
        {
            //Format
            WebMeta v = new WebMeta();
            v["Text"] = text;
            v["Type"] = "Footer";
            this._dataSource.Add(v);
        }
        public void AddFooter(string format, WebMeta data, UIStyle style)
        {
            WebMeta v = new WebMeta(data);
            v["Format"] = format;
            v["Type"] = "Footer";
            v.Put("Style", style);
            this._dataSource.Add(v);
        }
        /// <summary>
        /// 列表选择框，
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        /// <param name="value"></param>
        public WebMeta AddOption(string title, string code, string value, String text)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v.Put("Text", text).Put("DefaultValue", value);
            v["Type"] = "Option";
            v["Name"] = code;
            this._dataSource.Add(v);
            return v;
        }
        public WebMeta AddOption(string title, string code, string value)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v.Put("Text", value).Put("DefaultValue", value);
            v["Type"] = "Option";
            v["Name"] = code;
            this._dataSource.Add(v);
            return v;
        }
        public WebMeta AddColor(string title, string code, string value)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v.Put("DefaultValue", value);
            v["Type"] = "Option";
            v["Name"] = code;
            v.Put("Color", true);
            v.Command("System", "Color");
            this._dataSource.Add(v);
            return v;
        }
        public WebMeta AddIcon(string title, string code, String value)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v.Put("DefaultValue", value);
            v["Type"] = "Option";
            v["Name"] = code;
            v.Put("Icon", true);
            v.Command("System", "Icon");
            this._dataSource.Add(v);
            return v;
        }
        public WebMeta AddOption(string title, string code)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["Type"] = "Option";
            v["Name"] = code;
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 文件上传，
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddFile(string title, string Code, string defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["Type"] = "File";
            v["Name"] = Code;
            if (String.IsNullOrEmpty(defaultValue) == false)
            {
                v["DefaultValue"] = defaultValue;
            }
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 文件上传，
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddFiles(string title, string Code)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["Type"] = "Files";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 多行文本输入，
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddTextarea(string title, string Code, string defaultValue)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["Type"] = "Textarea";
            if (String.IsNullOrEmpty(defaultValue) == false)
            {
                v["DefaultValue"] = defaultValue;
            }
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 时间输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddDate(string title, string code, DateTime? defaultValue)
        {

            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["Name"] = code;
            if (defaultValue.HasValue)
            {
                v["DefaultValue"] = defaultValue.Value.ToString("yyyy-MM-dd");
            }
            v["Type"] = "Date";
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 增加文本输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddText(string title, string Code, string defaultValue = "")
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            v["DefaultValue"] = defaultValue;
            v["Type"] = "Text";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
            //this.dataSrouce.Set(Code, v);
        }
        /// <summary>
        /// 增加字典对说明
        /// </summary>
        /// <param name="items"></param>
        public WebMeta AddTextValue(string title, ListItemCollection items)
        {
            WebMeta v = new WebMeta();
            if (String.IsNullOrEmpty(title) == false)
                v["Title"] = title;
            v.Put("DataSource", items);

            v["Type"] = "TextValue";
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 增加字典对说明
        /// </summary>
        /// <param name="items"></param>
        public WebMeta AddTextValue(ListItemCollection items)
        {
            return this.AddTextValue(String.Empty, items);
        }
        /// <summary>
        /// 增加字典对说明
        /// </summary>
        /// <param name="items"></param>
        public WebMeta AddTextNameValue(ListItemCollection items)
        {
            return this.AddTextNameValue(String.Empty, items);
        }
        /// <summary>
        /// 增加字典对说明
        /// </summary>
        /// <param name="items"></param>
        public WebMeta AddTextNameValue(string title, ListItemCollection items)
        {
            WebMeta v = new WebMeta();
            if (String.IsNullOrEmpty(title) == false)
                v["Title"] = title;
            v.Put("DataSource", items);

            v["Type"] = "TextNameValue";
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 增加字典对说明
        /// </summary>
        /// <param name="items"></param>
        public ListItemCollection AddTextNameValue(string title)
        {
            var t = new ListItemCollection();
            this.AddTextNameValue(title, t);
            return t;
        }
        /// <summary>
        /// 增加字典对说明
        /// </summary>
        /// <param name="items"></param>
        public ListItemCollection AddTextNameValue()
        {
            var t = new ListItemCollection();
            this.AddTextNameValue(t);
            return t;
        }
        /// <summary>
        /// 增加字典对说明
        /// </summary>
        /// <param name="items"></param>
        public ListItemCollection AddTextValue(string title)
        {
            var t = new ListItemCollection();
            this.AddTextValue(title, t);
            return t;
        }
        /// <summary>
        /// 增加字典对说明
        /// </summary>
        /// <param name="items"></param>
        public ListItemCollection AddTextValue()
        {
            var t = new ListItemCollection();
            this.AddTextValue(t);
            return t;
        }

        /// <summary>
        /// 增加密码输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="defaultValue"></param>
        public WebMeta AddPassword(string title, string Code, bool isEnableMd5 = false)
        {
            WebMeta v = new WebMeta();
            v["Title"] = title;
            if (isEnableMd5)
            {
                v.Put("IsEnableMd5", true);
            }
            v["Type"] = "Password";
            v["Name"] = Code;
            this._dataSource.Add(v);
            return v;
        }
        public WebMeta AddRadioText(string name, string text)
        {
            WebMeta v = new WebMeta();
            v["Type"] = "RadioText";
            v["Name"] = name;
            v["Text"] = text;
            this._dataSource.Add(v);
            return v;
        }
        public WebMeta AddRadioText(string name, string text, string marker)
        {
            WebMeta v = new WebMeta();
            v["Type"] = "RadioText";
            v["Name"] = name;
            v["Text"] = text;
            v["Marker"] = marker;
            this._dataSource.Add(v);
            return v;
        }


        public WebMeta AddUIIcon(char icon, string title, string desc, UIClick click)
        {
            return this.Add(UI.UI.UI(icon, title, desc, click));
        }

        /// <summary>
        /// 增加时间选择框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        public WebMeta AddTime(string title, string code, int hour, int minute)
        {
            WebMeta v = new WebMeta();//"DataSource", ds.ToArray());
            v["Title"] = title;
            v["Type"] = "Time";
            v["DefaultValue"] = String.Format("{0:00}:{1:00}", hour, minute);
            v["Name"] = code;
            this._dataSource.Add(v);
            return v;

        }
        /// <summary>
        /// 增加时间选择框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        public WebMeta AddTime(string title, string code, DateTime? defaultValue)
        {
            WebMeta v = new WebMeta();//"DataSource", ds.ToArray());
            v["Title"] = title;
            v["Type"] = "Time";
            if (defaultValue.HasValue)
            {
                v["DefaultValue"] = defaultValue.Value.ToString("HH:mm");
            }
            v["Name"] = code;
            this._dataSource.Add(v);
            return v;
        }

        /// <summary>
        /// 增加选择框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Code"></param>
        /// <param name="items"></param>
        public ListItemCollection AddSelect(string title, string code)
        {
            ListItemCollection t = new ListItemCollection();
            AddSelect(title, code, t);
            return t;
        }
        /// <summary>
        /// 增加选择框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        /// <param name="items"></param>
        public WebMeta AddSelect(string title, string code, ListItemCollection items)
        {
            WebMeta v = new WebMeta();
            v.GetDictionary()["DataSource"] = items;
            v["Title"] = title;
            v["Type"] = "Select";
            v["Name"] = code;
            this._dataSource.Add(v);
            return v;
        }
        /// <summary>
        /// 增加复选框
        /// </summary>
        public ListItemCollection AddCheckBox(string title, string code, string defaultValue)
        {
            ListItemCollection t = new ListItemCollection();
            AddCheckBox(title, code, t, defaultValue);
            return t;
        }
        /// <summary>
        /// 增加复选框
        /// </summary>
        public ListItemCollection AddCheckBox(string title, string code)
        {
            ListItemCollection t = new ListItemCollection();
            AddCheckBox(title, code, t);
            return t;
        }
        public void AddCheckBox(string title, string code, ListItemCollection items, string defaultValue)
        {

            WebMeta v = new WebMeta();
            v.Put("DataSource", items);
            v["Title"] = title;
            if (String.IsNullOrEmpty(defaultValue) == false)
            {
                v["DefaultValue"] = defaultValue;
            }
            v["Type"] = "CheckboxGroup";
            v["Name"] = code;
            this._dataSource.Add(v);
        }
        /// <summary>
        /// 增加复选框
        /// </summary>
        public void AddCheckBox(string title, string code, ListItemCollection items)
        {
            AddCheckBox(title, code, items, null);
        }
        /// <summary>
        /// 增加单选择框
        /// </summary>
        public ListItemCollection AddRadio(string title, string code)
        {
            ListItemCollection t = new ListItemCollection();
            AddRadio(title, code, t);
            return t;
        }
        /// <summary>
        /// 增加单选择框
        /// </summary>
        public void AddRadio(string title, string code, ListItemCollection items)
        {
            WebMeta v = new WebMeta();
            v.Put("DataSource", items);
            v["Title"] = title;
            v["Type"] = "RadioGroup";
            v["Name"] = code;
            this._dataSource.Add(v);

        }
        /// <summary>
        /// 设置提交按钮名称，并设置当前位置
        /// </summary>
        /// <param name="btnName"></param>
        public void Submit(String btnName)
        {
            this.SubmitText = btnName;
            this._dataSource[this._dataSource.Count - 1].Put("Submit", "YES");
        }
        /// <summary>
        /// 不使用提交按钮
        /// </summary>
        public void HideSubmit()
        {
            _isHideSubmit = true;
            //this.Config.Put("Submit", false);
        }
        bool _isHideSubmit;
        /// <summary>
        /// 在最后一个控件下添加提交按钮
        /// </summary>
        public void Submit()
        {
            this._dataSource[this._dataSource.Count - 1].Put("Submit", "YES");
        }
        protected override void InitSubmit(WebRequest request)
        {
            var p = new WebMeta();
            p.Set("send", new WebMeta(request.Arguments).Put(KEY_DIALOG_ID, this.AsyncId));
            p["model"] = request.Model;
            p["cmd"] = request.Command;
            if (String.IsNullOrEmpty(SubmitText) == false)
            {
                p["text"] = SubmitText;
            }
            if (_isHideSubmit)
            {
                p.Put("hide", true);
            }
            this.Config.Put("Name", this.AsyncId);
            this.Config.Put("Submit", p);
        }
        /// <summary>
        /// 增加验收码输入框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        /// <param name="placeholder"></param>
        /// <returns></returns>
        public WebMeta AddVerify(String title, String code, String placeholder)
        {

            WebMeta v = new WebMeta();

            v["Title"] = title;
            v["Type"] = "Verify";
            v["Name"] = code;
            v["placeholder"] = placeholder;
            this._dataSource.Add(v);
            return v;

        }
        /// <summary>
        /// 事件提交方式参数配置，
        /// </summary>
        /// <param name="btnName">提交按钮名称</param>
        /// <param name="request">提交的当前指令有参数</param>
        /// <param name="colseEvent">对话框关闭的事件</param>
        public void Submit(string btnName, params string[] colseEvent)
        {
            this.SubmitText = btnName;
            if (colseEvent.Length > 0)
            {
                this.Config.Put("CloseEvent", String.Join(",", colseEvent));
            }
            Submit(btnName);
        }
        protected override void Initialization(WebContext context)
        {
            this.Config.Put("DataSource", _dataSource);
        }

        public List<WebMeta> DataSource
        {
            get
            {
                return _dataSource;
            }

        }

    }
}
